%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Interdisciplinary Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% none
%
%==========================================================================

%==========================================================================
% The function "createMissingDirs" checks if the directory where a given
% file resides exists and if not it tries to create it.
% INPUT: "fileNamePrefix" is a string specifying a prefix of a file name
% including full directory path.
% OUTPUT: "dirPath" returns the full directory path of the given file if
% the residency directory existed or was successfully created, and []
% otherwise.
%==========================================================================
function dirPath = createMissingDirs(fileNamePrefix)

% Determine the directory where the file with the given prefix resides.
indArr = regexp(fileNamePrefix, filesep);
if isempty(indArr)
    dirPath = '';
    return;
end
dirPath = fileNamePrefix(1:indArr(end));

% If the directory path does not exist, then create any missing directory.
if (exist(dirPath, 'dir') ~= 7)
    status = mkdir(dirPath);
    
    % Assert that the directory path was successfully created.
    assert((status ~= 0), ...
        'SoundRestoration:createMissingDirs:CanNotCreateDir', ...
        'Unable to create the non-existent directory %s.', dirPath);
end

return;
% end of the function "createMissingDirs"
